#include "movegenerator.h"
#include "common.h"


MoveGenerator::MoveGenerator()
{

}

MoveGenerator::~MoveGenerator()
{

}

MoveList MoveGenerator::GeneratePossibleMove( Byte matrix[15][15], long color )
{
    // get current chess area
    bool nochess = true;
    int min_x = 14, max_x = 0, min_y = 14, max_y = 0;
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 15; j++) {
            if (matrix[i][j] != kNoChess) {
                nochess = false;
                if (i < min_y) min_y = i;
                if (i > max_y) max_y = i;
                if (j < min_x) min_x = j;
                if (j > max_x) max_x = j;
            }
        }
    }

    if (nochess) {
        min_x = min_y = max_x = max_y = 7;
    }

    MoveList move_list;

    const int margin = 2;

    // calculate a area to drop chess
    int left = min_x - margin >= 0 ? min_x - margin : 0;
    int right = max_x + margin <= 14 ? max_x + margin : 14;
    int top = min_y - margin >= 0 ? min_y - margin : 0;
    int bottom = max_y + margin <= 14 ? max_y + margin : 14;

    for (int i = left; i <= right; i++) {
        for (int j = top; j <= bottom; j++) {
            if (kNoChess == matrix[j][i]){
                AIMove move = {i, j, color, 0};
                move_list.push_back(move);
            }
        }
    }

    return move_list;    
}